package com.lagou.controller;

import com.lagou.pojo.User;
import com.lagou.service.CodeFeignClient;
import com.lagou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/user")//  /api
@RefreshScope
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CodeFeignClient codeFeignClient;

    @Value("${spring.datasource.driver-class-name}")
    private String ori_driver_class_name;
    @Value("${spring.datasource.url}")
    private String ori_url;
    @Value("${spring.datasource.username}")
    private String ori_username;
    @Value("${spring.datasource.password}")
    private String ori_password;

    @Value("${datasource.driver-class-name}")
    private String cfg_driver_class_name;
    @Value("${datasource.url}")
    private String cfg_url;
    @Value("${datasource.username}")
    private String cfg_username;
    @Value("${datasource.password}")
    private String cfg_password;

    @GetMapping("/register/{email}/{password}/{code}")
    public boolean register(@PathVariable String email, @PathVariable String password, @PathVariable String code, HttpServletResponse response){
        ori_driver_class_name=cfg_driver_class_name;
        ori_url=cfg_url;
        ori_username=cfg_username;
        ori_password=cfg_password;

        System.out.println("注册："+email +password +code);
        //判断是否已注册
//        if(isRegistered(email)){
//            return false;
//        }
        //判断验证码的是否有效
        boolean validate = codeFeignClient.validate(email, code);
        if (!validate){
            return false;
        }

        String token_uuid = UUID.randomUUID().toString();
        //保存email password taken
        userService.saveUser(email,password,token_uuid);

        Cookie cookie = new Cookie("token",token_uuid);
        response.addCookie(cookie);
        return true;
    }

    @GetMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable String email){
        //System.out.println("注册："+email );
        User userByEmail = userService.findUserByEmail(email);
        if (userByEmail ==null){
            return false;
        }
        return true;
    }

    @GetMapping("/login/{email}/{password}")
    public String login(@PathVariable String email, @PathVariable String password, HttpServletResponse response) throws IOException {
        System.out.println(email+"=====>>>"+password);
        User user1 = userService.findUserByEmaiAndPwd(email,password);
        if (user1 ==null){
            return "";
        }else{
            //登录成功 跳转
            response.sendRedirect("http://localhost/api/user/info/"+user1.getToken());
        }

        return "";

//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.addObject("email","test001.@qq.com");
//        modelAndView.setViewName("welcome.html");
//        return modelAndView;
//        response.sendRedirect("http://localhost/static/welcome.html?email=123");
//        return "";
    }

    @GetMapping("/info/{token}")
    public String info(@PathVariable String token, HttpServletResponse response) throws IOException {
        //通过token 查询email
        User user1 = userService.findUserByToken(token);
        System.out.println(token);

        return user1.getEmail();
    }
}
